package com.example.purplestarcb.shoujizhushou;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by PurpleStarCB on 2015/3/20.
 */
public class SecurityFragment extends Fragment {
    private GridView listHome;
    private Adapter adapter;
    private String password;
    //定义密码
    private SharedPreferences sp;
    private static String[] listNames={
            "安全密码"

    };
    private  int[] ids={
            R.drawable.av_perm_group_accounts

    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.security_layout, container, false);

        GridView gridView =(GridView)view.findViewById(R.id.security_listHome);
        //注意这里
        adapter=new MyAdapt(view.getContext());
        sp=this.getActivity().getSharedPreferences("config",Context.MODE_PRIVATE);
        gridView.setAdapter((android.widget.ListAdapter) adapter);
        //设置GradView点击事件
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /**
             *
             * @param parent 传递gradView
             * @param view 传递view
             * @param position 传递位置
             * @param id 传递ID
             */
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    //安全密码
                    case 0:
                        //显示对话框
                        showLostFindDialog();
                        break;


                }
            }


        });
        return view;
    }

    private void showLostFindDialog() {
        //判断是否设置过密码
        if(isSetupPwd()){
            //密码已经设置，弹出输入密码框
            Log.i("consol","密码已经设置");
            showEnterDialog();
        }else{
            //密码未设置，弹出设置密码框
            Log.i("consol","密码未设置");
            showSetupPwdDialog();
        }

    }

    private EditText etSetupPwd;
    private EditText etEnterPwd;
    private EditText etSetupConfim;
    private Button ok;
    private Button cancel;
    private Dialog dialog;

    /**
     * 设置密码对话框
     */
    private void showSetupPwdDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        //自定义布局文件
        View view = View.inflate(getActivity(), R.layout.dialog_setup_password, null);

        etSetupPwd= (EditText) view.findViewById(R.id.et_setup_pwd);
        etSetupConfim= (EditText) view.findViewById(R.id.et_setup_confim);
        ok = (Button) view.findViewById(R.id.ok);
        cancel = (Button) view.findViewById(R.id.cancel);
        //设置取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取出密码 trim 为了去除空格
                String password=etSetupPwd.getText().toString().trim();
                String  passwordConfim=etSetupConfim.getText().toString().trim();
                if(TextUtils.isEmpty(password)||TextUtils.isEmpty(passwordConfim)){
                    Toast.makeText(getActivity(),"密码为空",Toast.LENGTH_LONG).show();
                    return;
                }
                //判断密码一致
                if (password.equals(passwordConfim)){
                    //保存密码，进入防盗页面
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putString("password",MD5Utils.md5Password(password));
                    editor.apply();
                    dialog.dismiss();
                    //进入防盗页面

                }else{
                    Toast.makeText(getActivity(),"密码不一致",Toast.LENGTH_LONG).show();
                    return;
                }
            }
        });
        builder.setView(view);
        dialog= builder.show();
    }

    /**
     * 输入密码对话框
     */
    private void showEnterDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        View view=View.inflate(getActivity(),R.layout.dialog_enter_password,null);
        etEnterPwd= (EditText) view.findViewById(R.id.et_enter_pwd);
        ok = (Button) view.findViewById(R.id.ok);
        cancel = (Button) view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("consol","取消");
                dialog.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取出密码 trim 为了去除空格
                String passwordl=etEnterPwd.getText().toString().trim();
                String save=sp.getString("password","");
                if(TextUtils.isEmpty(passwordl)){
                    return;
                }
                Log.i("consol",""+save+" wai"+passwordl);
                if (MD5Utils.md5Password(passwordl).equals(save)){
                    Log.i("consol",""+save+"   "+passwordl);
                    Intent intent=new Intent(getActivity(),LostFindActivity.class);
                    startActivity(intent);
                    dialog.dismiss();
                }
                else {
                    etEnterPwd.setText("");
                    dialog.dismiss();
                    return;
                }
            }
        });
        builder.setView(view);
        dialog =builder.show();
    }

    /**
     * 确认密码
     * @return
     */
    private Boolean isSetupPwd(){
        String password= sp.getString("password",null);

        return !TextUtils.isEmpty(password);
    }

    /**
     * 自定义Adapt
     */

    private class MyAdapt extends BaseAdapter {

        public MyAdapt(Context context) {

        }

        @Override
        public int getCount() {
            return listNames.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //注意这里是getActivity 不是AccelerateFragment.this
            View view=View.inflate(getActivity(),R.layout.security_list_item,null);
            ImageView iv_item= (ImageView) view.findViewById(R.id.iv_item);
            TextView tv_item= (TextView) view.findViewById(R.id.tv_item);

            tv_item.setText(listNames[position]);
            iv_item.setImageResource(ids[position]);
            return view;
        }
    }
}
